package cn.itrip.search.service;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.Data;
import cn.itrip.beans.vo.HotCityVo;
import cn.itrip.beans.vo.ReturnSearchHotel;
import cn.itrip.beans.vo.SearchQueryVo;
import cn.itrip.common.DtoUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.*;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@SuppressWarnings("all")
@Service
public class SearchServiceImpl implements SearchService{

    @Resource
    private SolrTemplate solrTemplate;

    @Override
    public Dto searchItripHotelPage(SearchQueryVo vo)throws Exception {
        Data data=new Data();
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("id");

        if(vo!=null){
            if(vo.getAscSort()!=null){
                Sort sort=new Sort(Sort.Direction.ASC,vo.getAscSort());//升叙排列字段
                query.addSort(sort);
            }
            if(vo.getDestination()!=null){
                criteria=criteria.and("destination").contains(vo.getDestination());// 目的地
            }

            if(vo.getDescSort()!=null){
                Sort sort=new Sort(Sort.Direction.DESC,vo.getDescSort());//倒叙排列字段
                query.addSort(sort);
            }
            if(vo.getFeatureIds()!=null){
                criteria=criteria.and("featureIds").contains(vo.getFeatureIds());//酒店特色 IDS
            }
            if (vo.getHotelLevel()!=null){
                criteria=criteria.and("hotelLevel").is(vo.getHotelLevel());//酒店等级
            }
            if(vo.getKeywords()!=null){
                criteria=criteria.and("keywords").contains(vo.getKeywords());//关键词
            }
           if(vo.getMaxPrice()!=null&&vo.getMinPrice()!=null){
                criteria=criteria.and("minPrice").between(vo.getMinPrice(),vo.getMaxPrice());//高低价
            }
            if(vo.getTradeAreaIds()!=null){
                criteria=criteria.and("tradeAreaIds").contains(vo.getTradeAreaIds());//商圈 IDS
            }
        }

        //查询
        query.addCriteria(criteria);
        Page<ReturnSearchHotel> page=solrTemplate.queryForPage(query, ReturnSearchHotel.class);
        List<ReturnSearchHotel> hotelList=page.getContent();

        //查询下标
        Integer count = hotelList.size();
        Integer pageno = 1;
        if (null != vo.getPageNo()) {
            pageno =vo.getPageNo();
        }
        int start = 0;
        if (vo.getPageNo() != null) {
            int num = vo.getPageNo() - 1;
            start = num * 5;
        }
        query.setOffset(start);
        int rows = 5;
        if (vo.getPageSize() != null) {
            rows = 5 *vo.getPageNo();
        }
        query.setRows(rows);
        hotelList = solrTemplate.queryForPage(query, ReturnSearchHotel.class).getContent();

        //设值分页处理
        data.setRows(hotelList);
        data.setTotal(count);
        Integer pageCount = count % 5 == 0 ? count / 5 : count / 5 + 1;
        data.setBeginPos((pageno - 1) * 5);
        data.setPageCount(pageCount);
        data.setCurPage(pageno);
        data.setPageSize(5);
        return DtoUtils.getSuccesDto(data);
    }

    @Override
    public Dto searchItripHotelListByHotCity(HotCityVo hotCityVo) throws Exception {
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("id");//Criteria   面向对象的 链编  返回值对象 是本身

        if (null !=hotCityVo.getCityId()){
            criteria=criteria.and("cityId").is(hotCityVo.getCityId());
        }
        //查询
        query.addCriteria(criteria);
        query.setRows(hotCityVo.getCount());
        Page<ReturnSearchHotel> page=solrTemplate.queryForPage(query, ReturnSearchHotel.class);
        List<ReturnSearchHotel> hotelList=page.getContent();
        return DtoUtils.getSuccesDto(hotelList);
    }
}
